package org.orbis.memoryblock.dictionary;

import java.nio.charset.Charset;
import org.orbis.memoryblock.BlockPackage;
import org.orbis.structure.LookupPointer;

public class DictionaryPackage implements BlockPackage{
	private LookupPointer currentOne, nextOne;
	private int frequency;
	private LookupPointer[] invertIndices;
	private int nameLength;
	private String name;
	private int cluster;
	private byte[] namebyte;

	private DictionaryPackage(){}

	public static DictionaryPackage setPackage(int indices, int block, int point){
		DictionaryPackage newpkg = new DictionaryPackage();
		newpkg.currentOne = LookupPointer.valueOf(block, point);
		newpkg.invertIndices = new LookupPointer[indices];
		newpkg.namebyte = new byte[0];
		return newpkg;
	}
	
	public static DictionaryPackage setPackage(String term, int indices, int cluster) {
		DictionaryPackage newpkg = new DictionaryPackage();
		newpkg.cluster = cluster;
		newpkg.invertIndices = new LookupPointer[indices];
		newpkg.name = term;
		newpkg.namebyte = term.getBytes(Charset.forName("UTF-8"));
		newpkg.nameLength = newpkg.namebyte.length;
		return newpkg;
	}

	public DictionaryPackage current(LookupPointer ptr){
		currentOne = ptr;
		return this;
	}

	public DictionaryPackage invIndex(LookupPointer invertIndex, int index){
		this.invertIndices[index] = invertIndex;
		return this;
	}

	public DictionaryPackage freq(int frequency){
		this.frequency = frequency;
		return this;
	}

	public DictionaryPackage name(String name){
		this.name = name;
		this.namebyte = name.getBytes(Charset.forName("UTF-8"));
		this.nameLength = this.namebyte.length;
		return this;
	}

	public DictionaryPackage nameraw(byte[] name){
		this.namebyte = name;
		this.nameLength = this.namebyte.length;
		this.name = new String(name, Charset.forName("UTF-8"));
		return this;
	}

	public DictionaryPackage next(LookupPointer nextOne){
		this.nextOne = nextOne;
		return this;
	}

	public DictionaryPackage cluster(int cluster){
		this.cluster = cluster;
		return this;
	}
	
	public void deleteInvIndex(int index) {
		this.invertIndices[index] = null;
	}

	public int getFrequency(){
		return frequency;
	}

	public LookupPointer getInvertIndex(int index){
		return invertIndices[index];
	}
	
	public String getName(){
		return name;
	}

	public byte[] getNameRaw(){
		return namebyte;
	}

	public int getNameLength(){
		return nameLength;
	}

	public LookupPointer getCurrent(){
		return currentOne;
	}

	public LookupPointer getNext(){
		return nextOne;
	}

	public int getCluster(){
		return cluster;
	}

	@Override
	public int length(){
		return PTR + DOCFREQ + (PTR * invertIndices.length) + STRLEN + namebyte.length;
	}
}
